package com.lg.erp.controller.yqy;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.SMenu;
import com.lg.erp.entity.SUser;
import com.lg.erp.entity.yqy.TProduct;
import com.lg.erp.service.IUserService;
import com.lg.erp.service.yqy.ITProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author qiu
 * @since 2021-04-19
 */
@RestController
public class TProductController {

    @Autowired
    private ITProductService productService;

    @Autowired
    private IUserService userService;
    
    private TProduct productId = new TProduct();

    /**
     * 商品分页查询
     * @param current
     * @return
     */
    @RequestMapping(value = "/pageProducts",method = RequestMethod.GET)
    public JsonResult getPageProducts(@RequestParam("current") Integer current){
        IPage<TProduct> page = new Page<>(current, 10);
        //IPage<TProduct> page = new Page<>(Integer.valueOf(current), 10);
        IPage<TProduct> iPage = productService.selectPageProducts(page);
        return new JsonResult(1,"商品分页查询",iPage);
    }

    @RequestMapping("/pageProductsByApply")
    public JsonResult getPageProductsByApply(@RequestParam("current") Integer current){
        IPage<TProduct> page = new Page<>(current, 10);
        IPage<TProduct> iPage = productService.selectProductByApply(page);
        return new JsonResult(1,"ok",iPage);
    }

    /**
     * 多条件分页查询
     * @param current
     * @param
     * @return
     */
    @RequestMapping(value = "/pageProductsByLike/{current}/{name}/{enterprise}",method = RequestMethod.GET)
    public JsonResult selectProductsByName(@PathVariable("current") String current,
                                           @PathVariable String name,@PathVariable String enterprise){
        //IPage<TProduct> page = new Page<>(current, 10);
        IPage<TProduct> page = new Page<>(Integer.valueOf(current), 10);
        IPage<TProduct> iPage = productService.selectPageProductByName(page,"%"+name+"%",enterprise);

        return new JsonResult(1,"ok",iPage);
    }

    /**
     * 查询单个商品信息
     * @param id
     * @return
     */
    @RequestMapping("product/{id}")
    public JsonResult getProductById(@PathVariable String id){
        TProduct product = productService.getById(id);
        return new JsonResult(1,"商品信息",product);
    }

    /**
     * 商品添加
     * @param product
     * @return
     */
    @PostMapping("product")
    public JsonResult addProduct(@RequestBody TProduct product){
        String s = UUID.randomUUID().toString().substring(0, 15).replace("-", "");
        product.setId(s);
        product.setProductNumber(s);
        product.setProductApplyStatus("0");
        product.setProductStatus("0");
        boolean save = productService.save(product);
        return new JsonResult(1,"商品添加成功",save);
    }

    /**
     * 修改商品信息( 申请状态 , 商品状态 , 商品信息 )
     * @param product
     * @return
     */
    @PutMapping("product")
    public JsonResult updateProduct(@RequestBody TProduct product){
        if (!product.getProductStatus().equals("1")){
            product.setProductApplyStatus("0");
            product.setProductStatus("0");
        }
        boolean b = productService.updateById(product);
        return new JsonResult(1,"修改商品成功",b);
    }
    @PutMapping("product2")
    public JsonResult updateProduct2(@RequestBody TProduct product){
        if (!product.getProductApplyStatus().equals("-1")){
            /*product.setProductApplyStatus("0");*/
            product.setProductStatus("0");
        }
        boolean b = productService.updateById(product);
        return new JsonResult(1,"修改商品成功",b);
    }


    /**
     * 删除商品
     * @return
     */
    @DeleteMapping("product/{id}")
    public JsonResult deleteProduct(@PathVariable String id){
        boolean b = productService.removeById(id);
        return new JsonResult(1,"删除商品成功",b);
    }

    @RequestMapping("productOne/{id}")
    public JsonResult selectProductOne(@PathVariable String id){
        productId.setId(id);
        return new JsonResult(1,"ok",null);
    }

    @RequestMapping("productOneById")
    public JsonResult selectProductById(){
        TProduct tProduct = productService.selectProductById2(productId.getId());
        return new JsonResult(1,"ok",tProduct);
    }
    @RequestMapping("product-companyId")
    public JsonResult selectProductByCompanyId(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<TProduct> list=new ArrayList<>();
        if(principal instanceof User) {
            User user = (User) principal;
            SUser one = userService.getOne(new QueryWrapper<SUser>().lambda().eq(SUser::getUsername, user.getUsername()));
           list = productService.list(new QueryWrapper<TProduct>().lambda().eq(TProduct::getCompanyId, one.getCompanyId()));
        }
            return new JsonResult(1,"ok",list);
    }




}
